//
//  ViewControllerCell.swift
//  movieInfo
//
//  Created by zhangkai on 2023/5/13.
//

import UIKit

class ViewControllerCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    var one:[Movie] = []
    var callBlock:((_ model:Movie)->Void)?

    
    private var collectionView: UICollectionView!
    
    func configData(datas:[Movie]){
        self.one = datas
        self.collectionView.reloadData()
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        flowLayout.itemSize = CGSize(width: 120, height: 220)
        flowLayout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ViewControllersubCell.self, forCellWithReuseIdentifier: "ViewControllersubCell")
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.one.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ViewControllersubCell", for: indexPath) as? ViewControllersubCell ?? ViewControllersubCell()
        
        let mov = self.one[indexPath.row]
//        cell.backgroundColor = .darkText
        
        
        if let path = mov.posterPath {
            cell.img.downloadImage(fromPath: path)
        }
        cell.nameLabel.text = mov.title ?? ""
        cell.stattLabel.text = "\(mov.voteAverage ?? 0.0)"
        
        
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        callBlock?(self.one[indexPath.row])
    }
    
    
    

}

class ViewControllersubCell:UICollectionViewCell{
    
    var img = MIImageView(frame: CGRect(x: 0, y: 0, width: 120, height: 180))
    var nameLabel = UILabel()
    var stattImg = UIImageView()
    var stattLabel = UILabel()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(img)
        img.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.width.equalTo(120)
            make.height.equalTo(180)
        }
        img.layer.masksToBounds = true
        img.contentMode = .scaleAspectFill
        
        nameLabel.text = "N/A"
        nameLabel.textColor = .black
        nameLabel.font = UIFont.boldSystemFont(ofSize: 16)
        contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
            make.top.equalTo(img.snp.bottom).offset(8)
        }
        
        stattImg.image = UIImage(named: "xingxing")
        contentView.addSubview(stattImg)
        stattImg.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(5)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
            make.width.equalTo(12)
            make.height.equalTo(12)
        }
        
        stattLabel.text = "N/A"
        stattLabel.textColor = .darkText
        stattLabel.font = UIFont.boldSystemFont(ofSize: 10)
        contentView.addSubview(stattLabel)
        stattLabel.snp.makeConstraints { make in
            make.centerY.equalTo(stattImg)
            make.left.equalTo(stattImg.snp.right)
         
        }
        
//
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
